@@ -63,7 +63,7 @@ module Agents |
||
63 | 63 |
predictions = page.css("//prediction") |
64 | 64 |
predictions.each do |pr| |
65 | 65 |
parent = pr.parent.parent |
66 |
- vals = {routeTitle: parent["routeTitle"], stopTag: parent["stopTag"]} |
|
66 |
+ vals = {"routeTitle" => parent["routeTitle"], "stopTag" => parent["stopTag"]} |
|
67 | 67 |
if pr["minutes"] && pr["minutes"].to_i < options["alert_window_in_minutes"].to_i |
68 | 68 |
vals = vals.merge Hash.from_xml(pr.to_xml) |
69 | 69 |
if not_already_in_memory?(vals) |
@@ -0,0 +1,57 @@ |
||
1 |
+ |
|
2 |
+require 'spec_helper' |
|
3 |
+require 'pry' |
|
4 |
+describe Agents::PublicTransportAgent do |
|
5 |
+ before do |
|
6 |
+ valid_params = { |
|
7 |
+ "name" => "sf muni agent", |
|
8 |
+ "options" => { |
|
9 |
+ "alert_window_in_minutes" => "20", |
|
10 |
+ "stops" => ['N|5221', 'N|5215'], |
|
11 |
+ "agency" => "sf-muni" |
|
12 |
+ } |
|
13 |
+ } |
|
14 |
+ @agent = Agents::PublicTransportAgent.new(valid_params) |
|
15 |
+ @agent.user = users(:bob) |
|
16 |
+ @agent.save! |
|
17 |
+ end |
|
18 |
+ describe "#check" do |
|
19 |
+ before do |
|
20 |
+ stub_request(:get, "http://webservices.nextbus.com/service/publicXMLFeed?a=sf-muni&command=predictionsForMultiStops&stops=N%7C5215"). |
|
21 |
+ with(:headers => {'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}). |
|
22 |
+ to_return(:status => 200, :body => File.read(Rails.root.join("spec/data_fixtures/public_transport_agent.xml")), :headers => {}) |
|
23 |
+ stub(Time).now {"2014-01-14 20:21:30 +0500".to_time} |
|
24 |
+ end |
|
25 |
+ it "should create 4 events" do |
|
26 |
+ lambda { @agent.check }.should change {@agent.events.count}.by(4) |
|
27 |
+ end |
|
28 |
+ it "should add 4 items to memory" do |
|
29 |
+ @agent.memory.should == {} |
|
30 |
+ @agent.check |
|
31 |
+ @agent.memory.should == {"existing_routes" => [{"stopTag"=>"5221", "tripTag"=>"5840324", "epochTime"=>"1389706393991", "currentTime"=>"2014-01-14 20:21:30 +0500"}, {"stopTag"=>"5221", "tripTag"=>"5840083", "epochTime"=>"1389706512784", "currentTime"=>"2014-01-14 20:21:30 +0500"}, {"stopTag"=>"5215", "tripTag"=>"5840324", "epochTime"=>"1389706282012", "currentTime"=>"2014-01-14 20:21:30 +0500"}, {"stopTag"=>"5215", "tripTag"=>"5840083", "epochTime"=>"1389706400805", "currentTime"=>"2014-01-14 20:21:30 +0500"}] |
|
32 |
+ } |
|
33 |
+ end |
|
34 |
+ it "should not create events twice" do |
|
35 |
+ lambda { @agent.check }.should change {@agent.events.count}.by(4) |
|
36 |
+ lambda { @agent.check }.should_not change {@agent.events.count} |
|
37 |
+ end |
|
38 |
+ end |
|
39 |
+ describe "validation" do |
|
40 |
+ it "should validate presence of stops" do |
|
41 |
+ @agent.options['stops'] = nil |
|
42 |
+ @agent.should_not be_valid |
|
43 |
+ end |
|
44 |
+ |
|
45 |
+ it "should validate presence of agency" do |
|
46 |
+ @agent.options['agency'] = "" |
|
47 |
+ @agent.should_not be_valid |
|
48 |
+ end |
|
49 |
+ |
|
50 |
+ it "should validate presence of alert_window_in_minutes" do |
|
51 |
+ @agent.options['alert_window_in_minutes'] = "" |
|
52 |
+ @agent.should_not be_valid |
|
53 |
+ end |
|
54 |
+ |
|
55 |
+ end |
|
56 |
+ |
|
57 |
+end |